﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using System.Data;
using System.Xml;
using ResxCrunch.Utils;
using System.IO;
using System.Resources;
using System.Collections;

namespace ResxCrunch.Business.Managers
{
    /// <summary>
    /// Xml manager class
    /// </summary>
    public class XmlMngr
    {
        #region Constants
        /// <summary>
        /// Logger declaration
        /// </summary>
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        /// <summary>
        /// Instance of the class. Lazy initialization
        /// </summary>
        private static readonly XmlMngr instance = new XmlMngr();

        /// <summary>
        /// The start part of the resource directory structure
        /// Ex. values-de
        /// </summary>
        private const string AndroidResourceDirectoryPrefix = "values";

        /// <summary>
        /// The separator character separating the prefix from the iso country code
        /// Ex. values-es
        /// </summary>
        private const string AndroidResourceDirectorySeparator = "-";
        #endregion Constants

        #region Attributes
        #endregion Attributes

        #region Initialization
        /// <summary>
        /// Prevents a default instance of the XmlMngr class from being created.
        /// </summary>
        private XmlMngr()
        {
        }

        /// <summary>
        /// Gets the reference to the object here.
        /// </summary>
        public static XmlMngr Instance
        {
            get
            {
                return instance;
            }
        }
        #endregion Initialization

        #region Public Methods
        /// <summary>
        /// Saves the datatable into an Xml file.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="fileName">Name of the file.</param>
        /// <returns>True on success</returns>
        public bool SaveXml(DataTable data, string fileName)
        {
            try
            {
                data.WriteXml(fileName, XmlWriteMode.WriteSchema);
                return true;
            }
            catch (Exception ex)
            {
                log.Error("Error saving Xml.", ex);
                return false;
            }
        }

        /// <summary>
        /// Reads the content of the datatable from an xml file.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <returns>Datatable with data
        /// Null on error</returns>
        public DataTable ReadXml(string fileName)
        {
            DataTable data = new DataTable("Data");

            try
            {
                data.ReadXml(fileName);
                return data;
            }
            catch (Exception ex)
            {
                log.Error("Could not read data.", ex);
                return null;
            }
        }
        #endregion Public Methods
    }
}